package com.example.wanandroid.activity.welcome

import android.app.Activity
import android.content.Context
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.wanandroid.R
import com.example.wanandroid.activity.main.home.BannerView
import com.example.wanandroid.entity.banner.Banner
import com.example.wanandroid.entity.banner.BannerData
import com.example.wanandroid.retrofit.RetrofitUtil
import com.example.wanandroid.room.AppRoomDatabase
import com.example.wanandroid.sharedpreferences.PrefsProperty
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.StringCallback
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.net.URL

object MyTest {
    fun testHandler(context: Context) {
        val handler = object : Handler(context.mainLooper) {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                Log.d("==", "handleMessage: ${Thread.currentThread().name}")
            }
        }

        Thread {
            var i = 0
            while (i < 5) {
                Thread.sleep(100)
                handler.sendEmptyMessage(i)
                i++
            }
        }.start()
    }

    fun testRetrofit() {
        RetrofitUtil.wanAndroidApi.getBannerData().enqueue(object : Callback<BannerData> {
            override fun onResponse(call: Call<BannerData>, response: Response<BannerData>) {
                val body = response.body()
                Log.d("==", "onResponse: $body")
            }

            override fun onFailure(call: Call<BannerData>, t: Throwable) {
                Log.d("==", "onFailure: ${t.message}")
            }

        })
    }

    fun testRoom() {
        val banner0 = Banner(
            "d",
            1,
            "https://dgss",
            1,
            1,
            "kkk",
            1,
            "ss"
        )
        AppRoomDatabase.getInstance().bannerDao().insert(banner0)

        val banners = AppRoomDatabase.getInstance().bannerDao().getBanners()
        Log.d("==", "$banners")

        val banner = banners[0]
        Log.d("==", "${AppRoomDatabase.getInstance().bannerDao().getBannerById(banner.id)}")

        AppRoomDatabase.getInstance().bannerDao().delete(banner)
        Log.d("==", "${AppRoomDatabase.getInstance().bannerDao().getBanners()}")

    }

    fun testBannerView(activity: Activity, contentView: ViewGroup) {
        val banners = ArrayList<View>()
        val imageView0 = ImageView(activity)
        imageView0.setImageResource(R.drawable.ic_launcher_foreground)
        banners.add(imageView0)
        val imageView1 = ImageView(activity)
        imageView1.setImageResource(R.mipmap.ic_launcher_round)
        banners.add(imageView1)
        val imageView2 = ImageView(activity)
        imageView2.setImageResource(R.mipmap.ic_launcher)
        banners.add(imageView2)

        val bannerView = BannerView(activity)
        bannerView.setBanners(banners)
        contentView.addView(
            bannerView,
            LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        )
    }

    fun testCustomView(activity: Activity, contentView: ViewGroup) {
        contentView.addView(
            CustomView(activity),
            LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        )
    }

    fun testClockView(activity: Activity, contentView: ViewGroup) {
        contentView.addView(
            ClockView(activity),
            LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        )
    }

    fun testOkHttpUtils() {
        OkHttpUtils.get().url("https://www.baidu.com/").build().execute(object : StringCallback() {
            override fun onError(p0: okhttp3.Call?, p1: Exception?, p2: Int) {
                Log.d("=======", p1?.message ?: "")
            }

            override fun onResponse(p0: String?, p1: Int) {
                Log.d("======", p0 ?: "")
            }
        })
    }

    fun testKetch() {
        //val id = MyApplication.ketch.download("")
        //MyApplication.ketch.cancel(id.id)
    }

    fun testPrefsProperty(context: Context) {
        var age: Int by PrefsProperty(context, "age", 0)
        age = 23

        Log.d(
            "==",
            context.getSharedPreferences("default", Context.MODE_PRIVATE).getInt("age", 7)
                .toString()
        )
    }

    @OptIn(DelicateCoroutinesApi::class)
    fun testCoroutine(activity: AppCompatActivity){
        GlobalScope.launch {
            delay(1000)
            Log.d("==test==",URL("http://www.baidu.com").readText())
            Log.d("==test==",Thread.currentThread().name) //DefaultDispatcher-worker-2
        }


        activity.lifecycleScope.launch {
            delay(1000)
            //URL("http://www.baidu.com").readText()  //NetworkOnMainThreadException
            Log.d("==test==",Thread.currentThread().name) //main
        }

    }
}